package com.tbit.main.controller;

import com.tbit.main.common.JsonResult;
import com.tbit.common.constant.MethodEnum;
import com.tbit.common.constant.ModuleEnum;
import com.tbit.main.constant.OperateLog;
import com.tbit.main.pojo.AccountUser;
import com.tbit.main.pojo.VO.StockMachineVO;
import com.tbit.main.service.StockMachineService;
import com.tbit.main.service.TokenService;
import com.tbit.main.util.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 入库车辆管理
 *
 * @Author: chen
 * @Description:
 * @Date: 创建于 9:12 2023/2/10
 */
@RestController
@RequestMapping("/stockMachine")
public class StockMachineController {

    @Autowired
    private StockMachineService stockMachineService;
    @Autowired
    private TokenService tokenService;

    /**
     * 分页查询
     *
     * @param token     鉴权秘钥
     * @param accountId 区域id
     * @param userCode  车辆编号
     * @param pageNO    页码数
     * @param rowCount  每页大小
     * @return
     */
    @RequestMapping("/queryPage")
    public JsonResult queryPage(String token, Integer accountId, String userCode, @RequestParam(defaultValue = "1") Integer pageNO, @RequestParam(defaultValue = "10") Integer rowCount) {
        if (accountId == null) {
            AccountUser accountUser = tokenService.getAccount(token);
            accountId = accountUser.getAccountId();
        }
        List<StockMachineVO> list = stockMachineService.queryPage(accountId, userCode, pageNO, rowCount);
        Integer total = stockMachineService.queryCount(accountId, userCode);
        return JsonResult.succ(total, list);
    }


    /**
     * 入库
     *
     * @param userCode 车辆编号
     * @param token    鉴权秘钥
     */
    @RequestMapping("/add")
    @OperateLog(module = ModuleEnum.STOCKMACHINE, methods = MethodEnum.STOCKMACHINE_ADD)
    public JsonResult add(String userCode, String token,String warehouseName,String warehouseId, Integer accountId) {
        Assert.hasLength(userCode, "Machine.userCode.notNull");
        AccountUser accountUser = tokenService.getAccount(token);
        stockMachineService.add(userCode, accountUser,warehouseName,warehouseId,accountId);
        return JsonResult.succ();
    }

    /**
     * 出库
     *
     * @param userCode 车辆编号
     * @param token    鉴权秘钥
     * @return
     */
    @RequestMapping("/del")
    @OperateLog(module = ModuleEnum.STOCKMACHINE, methods = MethodEnum.STOCKMACHINE_DEL)
    public JsonResult del(String userCode, String token, Integer accountId) {
        Assert.hasLength(userCode, "Machine.userCode.notNull");
        AccountUser accountUser = tokenService.getAccount(token);
        stockMachineService.del(userCode, accountUser,accountId);
        return JsonResult.succ();
    }


    /**
     * 批量入库
     *
     * @param userCodes 车辆编号
     * @param token     鉴权秘钥
     */
    @RequestMapping("/addBatch")
    @OperateLog(module = ModuleEnum.STOCKMACHINE, methods = MethodEnum.STOCKMACHINE_ADD)
    public JsonResult addBatch(@RequestParam("userCodes") List<String> userCodes, String token,String warehouseName,String warehouseId, Integer accountId) {
        Assert.notNull(userCodes, "Machine.userCode.notNull");
        AccountUser accountUser = tokenService.getAccount(token);
        return stockMachineService.addBatch(userCodes, accountUser,warehouseName,warehouseId, accountId);
    }

    /**
     * 批量出库
     *
     * @param userCodes 车辆编号
     * @param token     鉴权秘钥
     */
    @RequestMapping("/delBatch")
    @OperateLog(module = ModuleEnum.STOCKMACHINE, methods = MethodEnum.STOCKMACHINE_DEL)
    public JsonResult delBatch(@RequestParam("userCodes") List<String> userCodes, String token,Integer accountId) {
        Assert.notNull(userCodes, "Machine.userCode.notNull");
        AccountUser accountUser = tokenService.getAccount(token);
        stockMachineService.delBatch(userCodes, accountUser,accountId);
        return JsonResult.succ();
    }


    /**
     * 批量更换仓库
     *
     * @param userCodes 车辆编号
     * @param token     鉴权秘钥
     */
    @RequestMapping("/updateBatch")
    @OperateLog(module = ModuleEnum.STOCKMACHINE, methods = MethodEnum.STOCKMACHINE_UPDATE)
    public JsonResult updateBatch(@RequestParam("userCodes") List<String> userCodes, String token,String warehouseName,String warehouseId) {
         Assert.notNull(userCodes, "Machine.userCode.notNull");
        AccountUser accountUser = tokenService.getAccount(token);
        stockMachineService.updateBatch(userCodes, accountUser,warehouseName,warehouseId);
        return JsonResult.succ();
    }

    /**
     *  根据仓库id查询入库车辆
     */
    @RequestMapping("/selectByWarehouseId")
    public JsonResult selectByWarehouseId( String token,@RequestParam(name="warehouseId",required = false) List<Integer> warehouseId, String userCode, Integer accountId) {
        AccountUser accountUser = tokenService.getAccount(token);
        List<StockMachineVO> stockMachineVOS = stockMachineService.selectByWarehouseIdS(warehouseId,userCode,accountId);
        return JsonResult.succ(stockMachineVOS);
    }

    /**
     *  校验车辆是否合法
     */
    @RequestMapping("/checkMachine")
    public JsonResult checkMachine( String userCode) {
        return  stockMachineService.checkMachine(userCode);
    }
}
